#include "class_strategy_position.h"

class_strategy_position::class_strategy_position(std::string str1)
{
position=0;
value=0;
std::fill_n(dous, sizeof(dous)/sizeof(double), 0.0);
std::fill_n(ints, sizeof(ints)/sizeof(int), 0);
dz = str1;
std::string dz_back, dz_position,dz_value, dz_dous, dz_ints;
dz_back = dz + "/back.dat";
dz_position = dz + "/position.dat";
dz_value = dz + "/value.dat";
dz_dous = dz + "/dous.dat";
dz_ints = dz + "/ints.dat";
struct_return zz;
memset(&zz, 0, sizeof(struct_return));
std::ifstream wj(dz_back.c_str(), std::ios::app | std::ios::binary | std::ios::ate | std::ios::in);
int len = wj.tellg();
wj.seekg(0);
while (wj.tellg() < len)
{
wj.read(reinterpret_cast<char*>(&zz), sizeof(struct_return));
return_vector.push_back(zz);
}
wj.close();

wj.open(dz_position.c_str(), std::ios::app | std::ios::binary | std::ios::ate | std::ios::in);
wj.seekg(0);
wj.read(reinterpret_cast<char*>(&position), sizeof(int));
wj.close();

wj.open(dz_value.c_str(), std::ios::app | std::ios::binary | std::ios::ate | std::ios::in);
wj.seekg(0);
wj.read(reinterpret_cast<char*>(&value), sizeof(double));
wj.close();

std::ifstream infile(dz_dous.c_str(), std::ios::binary); // 打开二进制文件
if(infile.is_open())
{
infile.read(reinterpret_cast<char*>(dous), sizeof(dous)); // 读取数据到 dous1
}
infile.close(); // 关闭文件
infile.open(dz_ints.c_str(), std::ios::binary); // 打开二进制文件
if(infile.is_open())
{
infile.read(reinterpret_cast<char*>(ints), sizeof(ints)); // 读取数据到 dous1
}
infile.close(); // 关闭文件

}

class_strategy_position::~class_strategy_position()
{
write();
}

int class_strategy_position::write()
{

	std::string dz_back, dz_position,dz_value,dz_dous, dz_ints;
dz_back = dz + "/back.dat";
dz_position = dz + "/position.dat";
dz_value = dz + "/value.dat";
dz_dous = dz + "/dous.dat";
dz_ints = dz + "/ints.dat";

std::fstream wj;//声明写文件
wj.open(dz_back.c_str(), std::ios::binary | std::ios::out);
wj.seekp(0);
for (auto it = return_vector.begin(); it != return_vector.end(); ++it)
{
wj.write(reinterpret_cast<const char*>(&(*it)), sizeof(struct_return));
}
wj.close();

wj.open(dz_position.c_str(), std::ios::binary | std::ios::out);
wj.seekp(0);
wj.write(reinterpret_cast<const char*>(&position), sizeof(int));
wj.close();

wj.open(dz_value.c_str(), std::ios::binary | std::ios::out);
wj.seekp(0);
wj.write(reinterpret_cast<const char*>(&value), sizeof(double));
wj.close();


std::ofstream outfile(dz_dous.c_str(), std::ios::out | std::ios::binary | std::ios::trunc); // 以二进制模式打开文件用于写入
if(outfile.is_open())
{
outfile.write(reinterpret_cast<char*>(dous), sizeof(dous)); // 将数组内容写入文件
}
outfile.close(); // 关闭文件
outfile.open(dz_ints.c_str(), std::ios::out | std::ios::binary | std::ios::trunc); // 以二进制模式打开文件用于写入
if(outfile.is_open())
{
outfile.write(reinterpret_cast<char*>(ints), sizeof(ints)); // 将数组内容写入文件
}
outfile.close(); // 关闭文件
return 0;
}


//插入报单返回值
int class_strategy_position::insert(int id, int frontid, int sessionid, char orderref[], int tradingday,std::string name)
{
struct_return zz;
memset(&zz, 0, sizeof(struct_return));
zz.id = id;
zz.frontid = frontid;
zz.sessionid = sessionid;
strncpy(zz.orderref, orderref, sizeof(zz.orderref));
zz.tradingday = tradingday;
strncpy(zz.name, name.c_str(), name.size() );
return_vector.push_back(zz);
return 0;
}



